MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

Core utilities

Introduction#

Shared utilities behind the extension:

  • Logging with a factory and contextual prefixes
  • Message protocol definitions for cross-script communication
  • Shared types for safer messaging across the extension

Project structure#

The core utilities are organized under the assignment-solver/src/core directory and are used across the extension:

Core components#

Logger factory pattern#

The logging system provides a simple factory pattern for creating contextual loggers with consistent formatting and multiple log levels.

Key characteristics:

  • Contextual prefixes for easy identification of log sources
  • Standardized log levels: log, info, warn, error, debug
  • Cross-browser compatibility through console API
  • Lightweight implementation suitable for extension environments

Implementation details:

  • Background logger factory creates prefixed loggers for service components
  • Content script logger factory provides simplified logging for DOM interactions
  • Both factories return objects with standardized method signatures
  • Debug level logging is available but typically disabled in production builds

Message protocol definitions#

The messaging system defines a standardized protocol for communication between extension components, with built-in retry logic for reliable cross-browser operation.

Message types include:

  • Content script communication: PING, GET_PAGE_HTML, GET_PAGE_INFO, APPLY_ANSWERS, SUBMIT_ASSIGNMENT
  • Background communication: EXTRACT_HTML, CAPTURE_FULL_PAGE, GEMINI_REQUEST, GEMINI_DEBUG
  • Internal: SCROLL_INFO, SCROLL_TO, TAB_UPDATED

Message structure:

  • type: String identifier for the message category
  • payload: Optional structured data for the operation
  • Additional fields may be included based on the specific message type

Retry mechanism:

  • Configurable maximum retries (default: 3)
  • Exponential backoff with configurable base delay
  • Automatic detection of connection-related errors
  • Firefox-specific optimizations for initialization delays

Shared type definitions#

The type system provides JSDoc typedefs for consistent type safety across the extension, enabling better IDE support and runtime validation.

Core types defined:

  • Logger: Standardized logging interface with five log levels
  • Message: Base message structure with type and optional payload
  • ExtractedQuestion: Question structure with ID, type, text, choices, inputs, and answer
  • PageData: Complete page extraction result including HTML, images, and metadata
  • Screenshot: Image data with MIME type, base64 encoding, and positioning
  • ExtractionResult: Aggregated result of question extraction with submit button info

Type safety benefits:

  • IDE autocomplete and IntelliSense support
  • Consistent data structures across module boundaries
  • Clear documentation of expected interfaces
  • Runtime validation through property checking

Architecture overview#

Utilities split into logging, messaging, and shared types:

Detailed component analysis#

Logger implementation details#

Both logger factories implement the same interface pattern with slight variations for their intended use cases:

Logging usage patterns demonstrated in the extension:

  • Background service worker uses “Background” prefix for all operations
  • Content script uses “Content” prefix for DOM interactions
  • Gemini service logs API calls and response parsing
  • Router logs message routing decisions
  • Extractor and applicator log DOM manipulation operations

Message protocol implementation#

The messaging system provides a communication framework with the following key features:

Message handling patterns:

  • Firefox-specific optimizations for long-running handlers
  • Automatic channel management for asynchronous responses
  • Detailed error handling with descriptive error messages
  • Cross-browser compatibility through unified runtime adapter

Type safety mechanisms#

The type system keeps consistency across module boundaries through JSDoc typedefs and runtime validation patterns:

Type usage patterns:

  • Logger instances are passed as optional dependencies to services
  • Message objects follow the standardized structure across all components
  • Complex data structures are validated through property existence checks
  • Type definitions guide IDE autocomplete and provide compile-time documentation

Dependency analysis#

The core utilities have minimal dependencies and provide foundational services to the rest of the extension:

Dependency relationships:

  • Logger factory depends only on console API for output
  • Message system depends on browser.runtime for cross-script communication
  • Type definitions are purely documentation-based with no runtime impact
  • All consumers receive dependencies through constructor parameters or module imports

Performance considerations#

The core utilities stay small on purpose:

  • Logger factory creates lightweight objects with minimal memory footprint
  • Message protocol avoids unnecessary serialization overhead
  • Type definitions are processed at build time and have no runtime cost
  • Retry mechanism includes exponential backoff to minimize network pressure
  • Cross-browser compatibility is achieved through polyfill rather than runtime detection

Best practices for extension developers:

  • Use contextual prefixes to quickly identify log sources
  • Use the retry mechanism for transient connection failures
  • Use type definitions for better IDE support and fewer runtime errors
  • Keep log levels appropriate for the deployment environment

Troubleshooting guide#

Logging issues#

Common logging problems and solutions:

  • Missing log output: Verify logger factory is properly instantiated and methods are called
  • Incorrect prefixes: Ensure the correct logger factory is used for each component type
  • Debug logging not appearing: Debug level logging may be disabled in production builds

Message communication problems#

Diagnostic steps for messaging issues:

  • Verify message types match between sender and receiver
  • Check Firefox-specific message channel limitations
  • Monitor retry attempts for connection-related errors
  • Ensure handlers return appropriate responses for async operations

Type safety issues#

Resolving type-related problems:

  • Confirm all required properties are present in message objects
  • Verify type definitions match actual data structures
  • Use optional chaining for properties that may be undefined
  • Check for cross-browser differences in type support

Conclusion#

Logging and messaging helpers are intentionally small. Prefer adding a named helper over sprinkling raw browser APIs.